// const {remote} = require('electron')
// 渲染进程使用 remote 需要用 window.require 引入
const remote = window.require('@electron/remote')
const Menu = remote.Menu;
const MenuItem = remote.MenuItem;
const {ipcRenderer, shell,clipboard,nativeImage} = require('electron')
const path = require('path')

// 自定义右键菜单内容
let contextTemp = [
    {
        label: 'Run Code'
    },
    {
        label: '转到定义'
    },
    {
        type: 'separator'
    },
    {
        label: '其它功能',
        click: () => {
            console.log('其它功能项被点击了')
        }
    }
]
console.log("index.js load")
window.addEventListener('DOMContentLoaded', () => {
    // 点击按钮打开一个新的窗口
    let btn = document.getElementById('btn');

    btn.addEventListener('click', () => {
        let flag = false
        remote.getCurrentWindow().getChildWindows().forEach(item => {
            if (item.title === 'sub') {
                flag = true
            }
        })
        if (flag) {
            return
        }
        // 创建新的窗口
        let indexWindow = new remote.BrowserWindow({
            parent: remote.getCurrentWindow(), width: 400, height: 400, modal: true,// 模态窗口，子窗口存在时不允许操作父窗口
        })

        indexWindow.loadFile('list.html')
        indexWindow.show()
        console.log("btn click")
        indexWindow.on('close', () => {
            indexWindow = null
        })
    })
    let currentWindow = remote.getCurrentWindow();
    let min = document.getElementById('min')

    min.addEventListener('click', () => {
        currentWindow.minimize()
    })

    let max = document.getElementById('max')

    max.addEventListener('click', () => {
        if (!currentWindow.isMaximized()) {
            currentWindow.maximize()
        } else {
            // 回到原始状态
            currentWindow.restore()
        }


    })

    let close = document.getElementById('close')

    close.addEventListener('click', () => {
        currentWindow.close()
    })

//    窗口关闭提示
//    首先移除默认的关闭行为
    window.onbeforeunload = function () {
        console.log("窗口即将关闭")
        // return false
        return true
    }

//   自定义菜单
    let customMenu = document.getElementById('customMenu');
    customMenu.addEventListener('click', () => {
        let menu1 = new MenuItem({label: '文件', type: 'normal'})
        let menu2 = new MenuItem({label: '编辑', type: 'normal'})
        let menu = new Menu()
        menu.append(menu1)
        menu.append(menu2)
        Menu.setApplicationMenu(menu)
    })

//    自定义右键菜单
    window.addEventListener('contextmenu', (ev) => {
        // 阻止默认行为
        ev.preventDefault()
        let menu = Menu.buildFromTemplate(contextTemp)
        menu.popup({
            window: remote.getCurrentWindow()
        }, false)
    })


    let renderAsync = document.getElementById('renderAsync');
    let renderSynchronize = document.getElementById('renderSynchronize');
    // 采用异步的 api
    renderAsync.addEventListener('click',()=>{
        ipcRenderer.send('msg','当前是来自渲染进程的一条异步消息')
    })

    //    接收消息
    ipcRenderer.on('msg1',(ev,data)=>{
        console.log(data)
    })

//    采用同步的api
    renderSynchronize.addEventListener('click',()=>{
        let sendSync = ipcRenderer.sendSync('msgSync','当前是来自渲染进程的一条同步消息');
        console.log(sendSync)
    })

    // 渲染进程之间的通信
    let renderCommunicate = document.getElementById('renderCommunicate');
    renderCommunicate.addEventListener('click',()=>{
        //    将数据保存到 localStorage 中
        localStorage.setItem('renderCommunicate','渲染进程通信')
        ipcRenderer.send('renderCommunicate','open new window')

        clipboard.writeText('test');
        let text = clipboard.readText();
        // 将图片放置于剪切板当中的时候要求图片类型属于 nativeImage 实例
        let image = nativeImage.createFromPath('./msg.png')
        clipboard.writeImage(image)

    })
})
